package org.bbelovic.devel.exercises;

import java.util.List;

public class WeatherKataLineEvaluator implements LineEvaluator {

    public LineEvaluatorOutput evaluate(final LineEvaluatorInput input) {
        final List<String> lineTokens = input.getLineTokens();
        if (lineTokens.isEmpty()) {
            return new LineEvaluatorOutput(input.getCurrentValue(), input.getCurrentLine());
        }
        final int max = Integer.parseInt(lineTokens.get(1));
        final int min = Integer.parseInt(lineTokens.get(2));
        if ((max - min) < input.getCurrentValue()) {
            return new LineEvaluatorOutput(max - min, joinTokensToLine(lineTokens));
        }
        return new LineEvaluatorOutput(input.getCurrentValue(), input.getCurrentLine());
    }

    private String joinTokensToLine(List<String> lineTokens) {
        final StringBuilder sb = new StringBuilder();
        for (final String eachToken: lineTokens) {
            sb.append(eachToken)
              .append(" ");
        }
        return sb.toString();
    }

}
